<?php
// +----------------------------------------------------------------------
// | sfadmin - Suifeng Administration
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2020 http://suifeng.tech All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: suifeng <1787585212@qq.com>
// +----------------------------------------------------------------------

namespace suifeng\builder\html;

use suifeng\helper\HtmlHelper;

class Element
{
    /**
     * 标签名称。
     *
     * @var unknown
     */
    public $tag_name;

    /**
     * 属性数组。
     *
     * @var array
     */
    public $attributes = array();

    /**
     * 内容数组。
     *
     * @var array
     */
    public $children = array();

    /**
     * 构造方法。
     *
     * @param array $config
     */
    public function __construct($tag_name = null, $config = [])
    {
        $this->tag_name = $tag_name;
        foreach ($config as $key => $value) {
            if ($value !== null) {
                if ($key == 'content') {
                    $this->children[] = $value;
                } else {
                    $this->attributes[$key] = $value;
                }
            }
        }
    }

    /**
     * 输出HTML。
     *
     * @return string
     */
    public function render_html()
    {
        if ($this->tag_name) {
            return HtmlHelper::render_tag($this->tag_name, $this->attributes, $this->children);
        } else {
            return HtmlHelper::render_content($this->children);
        }
    }

    /**
     * 转换成字符串。
     *
     * @return string
     */
    public function __toString()
    {
        return $this->render_html();
    }

    /**
     * 加入子元素。
     */
    public function add($child)
    {
        if (is_string($child)) {
            $element = new Element();
            $element->children[] = $child;
            $child = $element;
        }
        $this->children[] = $child;
        return $child;
    }

    /**
     * 加入子元素。
     */
    public function child($child)
    {
        if (is_string($child)) {
            $element = new Element();
            $element->children[] = $child;
            $child = $element;
        }
        $this->children[] = $child;
        return $child;
    }

    /**
     * 内容。
     */
    public function content($content)
    {
        $this->children = [];
        $this->add($content);
        return $this;
    }

    /**
     * 动态的方法调用，以支持如下方式：
     * $element->cssClass('foo')
     *      ->width(8)
     *      ->height(9);
     *
     * @param unknown $name
     * @param unknown $args
     * @return \suifeng\builder\html\Element|mixed|NULL
     */
    public function __call($name, $args)
    {
        $alias_map = array(
            'cssClass'  =>  'class',
            'cssStyle'  =>  'style',
        );

        if (array_key_exists($name, $alias_map)) {
            $attr_name = $alias_map[$name];
        } else {
            $attr_name = $name;
        }

        if (count($args) > 0) {
            $this->attributes[$attr_name] = $args[0];
            return $this;
        } else {
            if (isset($this->attributes[$attr_name])) {
                return $this->attributes[$attr_name];
            } else {
                return null;
            }
        }
    }
}
